package com.bookmngsys.service.impl;



import com.bookmngsys.dao.BookDao;
import com.bookmngsys.dao.BookInfoDao;
import com.bookmngsys.dao.BookLocationDao;
import com.bookmngsys.dao.CategoryDao;
import com.bookmngsys.entity.Book;
import com.bookmngsys.entity.BookInfo;
import com.bookmngsys.entity.BookLocation;
import com.bookmngsys.entity.Category;
import com.bookmngsys.service.BookInfoService;
import com.bookmngsys.service.BookService;
import com.bookmngsys.utils.BarcodeUtil;
import com.bookmngsys.utils.CommonUtils;
import com.bookmngsys.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.bookmngsys.utils.BarcodeUtil.getBarCode;

@Service
public class BookInfoServiceImpl implements BookInfoService {
    @Autowired
    BookInfoDao bookInfoDao;


    @Autowired
    CategoryDao categoryDao;

    @Autowired
    BookLocationDao bookLocationDao;

    @Autowired
    BookDao bookDao;

    /**
     * 添加书籍
     */
    @Override
    public void addBookToBookInfo(String isbn, int number,HttpSession httpSession) throws Exception {
        BookInfo bookInfo = new BookInfo();
        String temp = isbn + number;

        bookInfo.setIsbn(isbn);
        String path = this.generateBarcode(isbn,number,httpSession);
        bookInfo.setBarcode(path);
        bookInfoDao.save(bookInfo);
    }


    @Override
    public List<BookInfo> getBooks() {
        return bookInfoDao.findAll();
    }


    @Override
    public List<BookInfo> getBooksByIsbn(String isbn) {
        return bookInfoDao.findBookByIsbn(isbn);
    }

    @Override
    public BookInfo getBookById(int bookId) {
        return bookInfoDao.findBookByBookId(bookId);
    }

    @Override
    public void deleteBookByBookId(int bookId) {
        bookInfoDao.deleteById(String.valueOf(bookId));
    }

    @Override
    public String generateBarcode(String isbn, int bookid, HttpSession httpSession) throws Exception {
        BufferedImage image = BarcodeUtil.insertWords(getBarCode(isbn + "_" + bookid), isbn + "_" + bookid);
        String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        System.out.println(path);
        String dirPath = path;
        File tempFile = new File(  dirPath + isbn + "_" + bookid + ".jpg");
        ImageIO.write(image, "jpg", tempFile);
        String path1 =  isbn + "_" + bookid + ".jpg";
        return path1;
    }

    @Override
    public List<BookInfo> searchByReaderId(int readerId) {
        return bookInfoDao.findBorrowBookByReaderId(readerId);
    }

    @Override
    public List<BookInfo> searchDamageBooks() {
        List<BookInfo> bookInfos =  bookInfoDao.findAll();
        List<BookInfo> bookInfosRes = new ArrayList<>();
        for(BookInfo bookInfo:bookInfos){
            if(bookInfo.getDamage() == 0){
                bookInfosRes.add(bookInfo);
            }
        }
        return bookInfosRes;
    }

    @Override
    public void setDamageBook(int id,int status) throws Exception {
        BookInfo bookInfo = bookInfoDao.findBookByBookId(id);
        Integer readerId;
        if((readerId  = CommonUtils.getBorrowReaderId(bookInfo)) != null){
            throw new Exception("bookId " + id + "is borrowed by " + readerId + " please set later");
        }else if((readerId  = CommonUtils.getReserveReaderId(bookInfo)) != null){
            throw new Exception("bookId " + id + "is reserved by " + readerId + " please set later");
        }else if(!(status == 0 || status == 1)){
            throw new Exception("status only can be 0 or 1");
        }
        bookDao.reduceBookNum(bookInfo.getIsbn());
        bookInfoDao.setDamage(id,status);
    }

    @Override
    public void setLostBook(int id,int status) throws Exception {
        BookInfo bookInfo = bookInfoDao.findBookByBookId(id);
        Integer readerId;
        if((readerId  = CommonUtils.getBorrowReaderId(bookInfo)) != null){
            throw new Exception("bookId " + id + "is borrowed by " + readerId + " please set later");
        }else if((readerId  = CommonUtils.getReserveReaderId(bookInfo)) != null){
            throw new Exception("bookId " + id + "is reserved by " + readerId + " please set later");
        }else if(!(status == 0 || status == 1)){
            throw new Exception("status only can be 0 or 1");
        }
        bookDao.reduceBookNum(bookInfo.getIsbn());
        bookInfoDao.setLost(id,status);
    }

    @Override
    public List<BookInfo> searchLostBooks() {
        List<BookInfo> bookInfos =  bookInfoDao.findAll();
        List<BookInfo> bookInfosRes = new ArrayList<>();
        for(BookInfo bookInfo:bookInfos){
            if(bookInfo.getLost() == 0){
                bookInfosRes.add(bookInfo);
            }
        }
        return bookInfosRes;
    }


}
